/**
 * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lucky.blessing.ui;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lucky.blessing.R;
import com.lucky.blessing.data.UserSumUrl;
import com.lucky.blessing.data.result.UserSumResult;
import com.lucky.blessing.dialog.PayPassDialog;
import com.lucky.blessing.dialog.PayPassView;
import com.lucky.blessing.network.AppNetworkUtils;
import com.lucky.blessing.utils.UserInfoUtils;
import com.hyphenate.easeui.widget.EaseAlertDialog;
import com.mathum.baseapp.network.NetworkCallback;
import com.mathum.baseapp.network.NetworkUtils;

public class AddPayAccountActivity extends BaseActivity{
	private EditText editText;
	private RelativeLayout searchedUserLayout;
	private TextView nameText;
	private Button searchBtn;
	private String toAddUsername;
	private String forFriendID;
	private ProgressDialog progressDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.em_activity_add_payaccount);
		
		editText = (EditText) findViewById(R.id.edit_note);
		String strUserName = getResources().getString(R.string.user_name);
		editText.setHint(strUserName);
		searchedUserLayout = (RelativeLayout) findViewById(R.id.ll_user);
		nameText = (TextView) findViewById(R.id.name);
		searchBtn = (Button) findViewById(R.id.search);
	}
	
	
	/**
	 * search contact
	 * @param v
	 */
	public void searchPayContact(View v) {
		final String userName = editText.getText().toString().toLowerCase();
		String saveText = searchBtn.getText().toString();
		
		if (getString(R.string.button_search).equals(saveText)) {
			toAddUsername = userName;
			if(TextUtils.isEmpty(userName)) {
				new EaseAlertDialog(this, R.string.Please_enter_a_username).show();
				return;
			}
			
			// TODO you can search the user from your app server here.
			String selfName=UserInfoUtils.getUserName(AddPayAccountActivity.this);
			if(selfName.equals(userName)){
				new EaseAlertDialog(this, R.string.not_add_myself).show();
				return;
			}

			NetworkUtils.fetchInfo(AppNetworkUtils.initRetrofitApi().getUserInfo(userName),
					new NetworkCallback<UserSumResult>() {
						@Override
						public void onSuccess(UserSumResult userSumResult) {
							UserSumUrl userSumUrl=userSumResult.getUrl();
							forFriendID=userSumUrl.getId();
							searchedUserLayout.setVisibility(View.VISIBLE);
							nameText.setText(toAddUsername);
						}

						@Override
						public void onFailure(Throwable t) {
							Toast.makeText(AddPayAccountActivity.this, t.getMessage(), Toast.LENGTH_LONG).show();
						}
					});
			//show the userame and add button if user exist

			
		} 
	}	
	
	/**
	 *  add contact
	 * @param view
	 */
	public void addPayContact(View view){
		final EditText et = new EditText(AddPayAccountActivity.this);
		et.setInputType(InputType.TYPE_CLASS_NUMBER);
		new AlertDialog.Builder(AddPayAccountActivity.this).setTitle("请设置付款金额")
				.setView(et)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialogInterface, int i) {
						String user_sum_add=et.getText().toString();
						String sum_foruserID= UserInfoUtils.getAppUserId(AddPayAccountActivity.this);
						PayPassDialog payPassDialog=new PayPassDialog(AddPayAccountActivity.this);
						payPassDialog.getPayViewPass().setPayClickListener(new PayPassView.OnPayClickListener() {
							@Override
							public void onPassFinish(String passContent) {
								String payPassWord=UserInfoUtils.getPayPassWord(AddPayAccountActivity.this);
								if(payPassWord.equals(passContent)){
									payPassDialog.dismiss();
									//按下确定键后的事件
									NetworkUtils.fetchInfo(AppNetworkUtils.initRetrofitApi().getCodeToUser(user_sum_add,sum_foruserID,forFriendID),
											new NetworkCallback<UserSumResult>() {
												@Override
												public void onSuccess(UserSumResult userSumResult) {
													UserSumUrl userSumUrl=userSumResult.getUrl();
													String sumString=String.valueOf(userSumUrl.getUser_sum());
													UserInfoUtils.setUserSum(sumString,AddPayAccountActivity.this);
													Toast.makeText(AddPayAccountActivity.this, "交易成功，付款金额为:"+user_sum_add+"元",Toast.LENGTH_LONG).show();
												}

												@Override
												public void onFailure(Throwable t) {
													Toast.makeText(AddPayAccountActivity.this, t.getMessage(), Toast.LENGTH_LONG).show();
												}
											});
								}else{
									payPassDialog.dismiss();
									Toast.makeText(AddPayAccountActivity.this, "支付密码错误", Toast.LENGTH_LONG).show();
								}

							}

							@Override
							public void onPayClose() {
								payPassDialog.dismiss();
							}

							@Override
							public void onPayForget() {
								payPassDialog.dismiss();
							}
						});

					}
				}).setNegativeButton("取消",null).show();

	}
	
	public void back(View v) {
		finish();
	}
}
